#Include "Protheus.ch"
#Include "TbiConn.ch"

//--------------------------------------------------------------------------------------------------------
/*/{Protheus.doc} TLDJ0020
Agrupa pesagens por lotes.
        
@author Everton Alves
@version 1.0
@since 08/07/2014
@return Nil
@obs Lotes vazios devem ser informados como "NULO" 
@sample
..........
Integracao Protheus X MWS Easy 
..........
/*/
//--------------------------------------------------------------------------------------------------------
User Function TLDJ0020()

Local cAlias    := ""
Local cQuery    := ""
Local nRecCount := 0

RpcSetType(3)
PREPARE ENVIRONMENT EMPRESA "99" FILIAL "01"
ConOut("[" + DToC(Date()) + " " + Time() + "] ####### INTEGRACAO TOLEDO - AGRUPANDO LOTES  ####### ")
cAlias := GetNextAlias()
cQuery += " SELECT Z8_MOVIDT, Z8_ORDNOM, Z8_ITMNMCD, Z8_LOTNOM, Z8_MOVQTD, Z8_MOVPES, Z8_TAREMB1" + CRLF
cQuery += " FROM " + RetSqlTab("SZ8") + CRLF
cQuery += " WHERE Z8_FLXNOM = 'FLUXO30'" + CRLF
cQuery += 	" AND Z8_MOVSNT = '1271'" + CRLF
cQuery += 	" AND Z8_LOCNOM = 'LOC40'" + CRLF
cQuery += 	" AND Z8_STATUS = '00'" + CRLF
cQuery += 	" AND " + RetSqlFil("SZ8") + CRLF
cQuery += 	" AND " + RetSqlDel("SZ8") + CRLF
cQuery += " ORDER BY Z8_ORDNOM, Z8_ITMNMCD, Z8_LOTNOM"
//MemoWrite("C:\RelMicrosiga\TLDJ020PRO.txt", cQuery)
cQuery := ChangeQuery(cQuery)    // normaliza a consulta

IF (Select(cAlias) <> 0)
	dbSelectArea(cAlias)
	(cAlias)->(dbCloseArea())
EndIF
DbUseArea(.T., "TOPCONN", TCGenQry( , , cQuery), cAlias, .F., .T.)    // executa a consulta
Count To nRecCount

IF nRecCount == 0
	ConOut("[" + DToC(Date()) + " " + Time() + "] NAO HA LOTES PARA AGRUPAR.")
	ConOut("[" + DToC(Date()) + " " + Time() + "] ####### AGRUPANDO LOTES - FIM DO PROCESSAMENTO ####### ")
	Return Nil
EndIF

(cAlias)->(dbGoTop())	
dbSelectArea("SZ9")
SZ9->(dbSetOrder(1))
dbSelectArea("SZ0")
SZ0->(dbSetOrder(1))
dbSelectArea("SZ8")
SZ8->(dbSetOrder(1)) // Z8_FILIAL+Z8_MOVIDT

While !(cAlias)->(Eof())
	SZ0->(dbSeek(xFilial("SZ0") + (cAlias)->Z8_ITMNMCD))
	IF SZ9->(dbSeek(xFilial("SZ9") + Upper((cAlias)->(Z8_ORDNOM+Z8_ITMNMCD+Z8_LOTNOM))))
		ConOut("[" + DToC(Date()) + " " + Time() + "] AGRUPANDO ITEM (L-" + RTrim(Upper((cAlias)->Z8_LOTNOM)) + "-O-" + RTrim((cAlias)->Z8_ORDNOM) + "-P-" + RTrim((cAlias)->Z8_ITMNMCD) + ")")
		RecLock("SZ9", .F.)
		SZ9->Z9_MOVQTD  += (cAlias)->Z8_MOVQTD
		SZ9->Z9_MOVPES  += (cAlias)->Z8_MOVPES
		SZ9->Z9_PESBRU  += (cAlias)->Z8_MOVPES + (cAlias)->Z8_TAREMB1
		SZ9->Z9_TAREMB1 += (cAlias)->Z8_TAREMB1
		SZ9->Z9_QTDFRM  += SZ0->Z0_FORMAS
		SZ9->Z9_QTDCXS++
		MsUnlock()
	Else
		ConOut("[" + DToC(Date()) + " " + Time() + "] INCLUINDO ITEM (L-" + RTrim(Upper((cAlias)->Z8_LOTNOM)) + "-O-" + RTrim((cAlias)->Z8_ORDNOM) + "-P-" + RTrim((cAlias)->Z8_ITMNMCD) + ")") 
		RecLock("SZ9", .T.)
		SZ9->Z9_FILIAL  := xFilial("SZ9")
		SZ9->Z9_ORDNOM  := (cAlias)->Z8_ORDNOM
		SZ9->Z9_ITMNMCD := (cAlias)->Z8_ITMNMCD
		SZ9->Z9_LOTNOM  := Upper((cAlias)->Z8_LOTNOM)
		SZ9->Z9_MOVQTD  := (cAlias)->Z8_MOVQTD
		SZ9->Z9_MOVPES  := (cAlias)->Z8_MOVPES
		SZ9->Z9_PESBRU  := (cAlias)->Z8_MOVPES + (cAlias)->Z8_TAREMB1
		SZ9->Z9_TAREMB1 := (cAlias)->Z8_TAREMB1
		SZ9->Z9_QTDFRM  := SZ0->Z0_FORMAS
		SZ9->Z9_QTDCXS  := 1
		SZ9->Z9_STATUS  := "0"
		SZ9->Z9_DATA    := dDataBase
		MsUnlock()
	EndIF

	IF SZ8->(dbSeek(xFilial("SZ8") + (cAlias)->Z8_MOVIDT))
		RecLock("SZ8", .F.)
			SZ8->Z8_STATUS := "01"  // Processado
		MsUnlock()
	EndIF
	(cAlias)->(dbSkip())
End

ConOut("[" + DToC(Date()) + " " + Time() + "] ####### AGRUPANDO LOTES - FIM DO PROCESSAMENTO #######")
(cAlias)->(dbCloseArea())
SZ9->(dbCloseArea())
SZ0->(dbCloseArea())
SZ8->(dbCloseArea())
RESET ENVIRONMENT
Return Nil
//--------------------------------------------------------------------------------------------------------